var api = require('./../../config/api.js');
var app = getApp();
var util = require('./../../utils/util.js');


Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    working: [],
    jobDone: [],
    payInfo: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.loadUndone();
  },

  // 加载待完成
  loadUndone: function(){
    var that = this;
    var unDoneUrl = api.UserOrderList + '/undone/' + app.globalData.userInfo.token;
    util.myRequest(unDoneUrl, false, function (data) {
      for(var i=0;i<data.data.length;i++){
        if (data.data[i].aoType == 'shop'){
          var aoTakeInfo = JSON.parse(data.data[i].aoTakeInfo);
          data.data[i].stName = aoTakeInfo.shop_name;
          data.data[i].ceName = aoTakeInfo.goods_name;
          data.data[i].aaMobile = aoTakeInfo.cellphone;
          data.data[i].aaAddress = aoTakeInfo.address;
          data.data[i].ssName = '';
        }
      }
      that.setData({ working: data.data });
      that.loadDone();
    });
  },

  // 已完成
  loadDone: function(){
    var that = this;
    var doneUrl = api.UserOrderList + '/done/' + app.globalData.userInfo.token;
    util.myRequest(doneUrl, false, function (data) {
      for (var i = 0; i < data.data.length; i++) {
        if (data.data[i].aoType == 'shop') {
          var aoTakeInfo = JSON.parse(data.data[i].aoTakeInfo);
          data.data[i].stName = aoTakeInfo.shop;
          data.data[i].ceName = aoTakeInfo.goods_name;
          data.data[i].aaMobile = aoTakeInfo.cellphone;
          data.data[i].aaAddress = aoTakeInfo.address;
          data.data[i].aaName = aoTakeInfo.shop;
          data.data[i].ssName = '';
        }
      }
      that.setData({ jobDone: data.data });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return app.globalData.shareInfo;
  },

  /**
   * 滑动切换
   */
  swiperTab: function(e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },

  /**
   * 点击切换
   */
  clickTab: function(e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },

  // 查看详细信息
  goDetail: function (e){
    wx.navigateTo({
      url: './../detail/detail?aoId=' + e.target.id
    })
  },

  // 取消订单
  channel: function(e) {
    var that = this;
    wx.showModal({
      title: '你确定要取消订单吗',
      content: '',
      cancelText: '不了',
      confirmText: '确定',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定');
          that.affirmOrder(e.target.id, -1);
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  // 确认取货
  getGoods: function (e) {
    // e.target.id
    var that = this;
    wx.showModal({
      title: '确定已收货？',
      content: '如有问题请在建议里反馈',
      cancelText: '没有',
      confirmText: '已收到',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定');
          that.affirmOrder(e.target.id, 3);
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  affirmOrder: function (aoId, status){
    var that = this;
    var postData = {};
    postData.token = app.globalData.userInfo.token;
    postData.aoId = aoId;
    postData.status = status;
    postData.chancelReason = "";

    util.myRequest(api.AffirmOrder, postData, function(data){
      that.loadUndone();
    });
  },

  // 确认支付
  doPay: function (e) {
    // e.target.id
    var that = this;
    var postData = {};
    postData.token = app.globalData.userInfo.token;
    postData.aoId = e.target.id;
    util.myRequest(api.PayOrder, postData, function(data){
      that.wxPay(data.data);
    });
  },

  wxPay: function (payInfo) {
    var that = this;
    wx.requestPayment({
      'timeStamp': payInfo.timeStamp,
      'nonceStr': payInfo.nonceStr,
      'package': payInfo.package,
      'signType': 'MD5',
      'paySign': payInfo.paySign,
      'success': function (res) {
        wx.showToast({
          title: '支付成功!',
          duration: 2000
        })

        setTimeout(function () {
          that.loadUndone();
        }, 2000);
      },
      'fail': function (res) {
        wx.showToast({
          title: '支付失败',
          icon: 'success',
          duration: 1500
        })
      }
    })
  }
})